package com.nbjtjc.safe.mapper.sys;

import com.nbjtjc.safe.bean.Page;
import com.nbjtjc.safe.model.sys.SysDept;
import com.nbjtjc.util.MyMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface SysDeptMapper extends MyMapper<SysDept> {

    //    @ResultMap("detailMap")
    @ResultMap("detailMapNoUsers")
    @Select("select * from sys_dept where parent_id=#{parentId} order by type desc, num asc")
    List<SysDept> findByParentId(@Param("parentId") int parentId);

    @ResultMap("detailMapNoUsers")
    @Select("select * from sys_dept where parent_id=#{parentId} and type=#{deptType} order by type desc, num asc")
    List<SysDept> findOnlyDeptByParentId(@Param("parentId") int parentId
            , @Param("deptType") int deptType);

    @Select("select * from sys_dept where parent_id=#{parentId} order by type asc, num asc")
    List<SysDept> findByParentIdNoUser(@Param("parentId") int parentId);

    List<SysDept> search(@Param("dept") SysDept sysDept, @Param("page") Page page);

    @Select("select sys_dept.name from sys_dept where id=#{deptId}")
    String findNameById(@Param("deptId") int id);

    @Select("select sys_dept.id from sys_dept where type=#{type} and parent_id=#{companyId}")
    List<Integer> getSubCompanyId(@Param("companyId") int companyId, @Param("type") int type);

    @Select("select * from sys_dept where type=1 and parent_id=0 ")
    List<SysDept> getTopCompany();

    @Select("select max(num) as num ,id from sys_dept where parent_id=#{parentId} and num<#{num}" +
            " and type=#{type} GROUP BY id ORDER BY num DESC ")
    List<SysDept> getUpDeptNum(@Param("parentId") int parentId, @Param("num") int num
            , @Param("type") int type);

    @Select("select min(num) as num ,id from sys_dept where parent_id=#{parentId} and num>#{num}" +
            " and type=#{type} GROUP BY id ORDER BY num asc")
    List<SysDept> getDownDeptNum(@Param("parentId") int parentId, @Param("num") int num
            , @Param("type") int type);

    @Select("select * from sys_dept where parent_id=#{parentId} and type =#{type} ")
    List<SysDept> getSubCompanyByParentId(@Param("parentId") int parentId, @Param("type") int type);


    @Select("<script>" +
            "select id,sys_dept.name from" +
            " sys_dept where id in " +
            " <foreach item='deptId' index='index' collection='deptIds'      open='(' separator=',' close=')'> " +
            "#{deptId}" +
            " </foreach> " +
            "</script>")
    List<SysDept> getSysDeptName(@Param("deptIds") List<Integer> deptIds);

    @Select("<script>" +
            "select sys_dept.id,sys_dept.name,sys_dept.type,sys_dept.parent_id from" +
            " sys_dept where parent_id in " +
            " <foreach item='deptId' index='index' collection='deptIds'      open='(' separator=',' close=')'> " +
            "#{deptId}" +
            " </foreach> " +
            "</script>")
    List<SysDept> getLowerDepts(@Param("deptIds") List<Integer> deptIds);


}